package org.example.smartbuy.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.smartbuy.entity.Products;
import org.example.smartbuy.mapper.CartMapper;
import org.example.smartbuy.entity.Cart;
import org.example.smartbuy.service.ICartService;
import org.example.smartbuy.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车服务实现类
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper,Cart> implements ICartService {

    @Autowired
    private CartMapper cartMapper; // 注入 CartMapper

    @Autowired
    private IProductsService productsService; // 注入 ProductsService

    /**
     * 添加商品到购物车
     */
    @Override
    public void addToCart(Cart cart) {
        cartMapper.insert(cart);
    }

    /**
     * 获取购物车某个商品的详情
     */
    @Override
    public Products getCartDetail(int cartId) {
        Cart cart = cartMapper.selectById(cartId);
        return productsService.getById(cart.getProductId());
    }

    /**
     * 更新购物车中商品的数量
     */
    @Override
    public Cart updateCartItem(Cart cart) {
        cartMapper.updateById(cart);
        return cart;
    }

    /**
     * 删除购物车中的商品
     */
    @Override
    public void deleteCartItem(int cartId) {
        cartMapper.deleteById(cartId);
    }

    /**
     * 获取用户的购物车列表
     */
    @Override
    public List<Cart> getUserCartList(int userId) {
        QueryWrapper<Cart> cartQueryWrapper = new QueryWrapper<>();
        cartQueryWrapper.eq("user_id", userId);
        return cartMapper.selectList(cartQueryWrapper);
    }
}
